/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Whitemice.ZOGI;

namespace Whitemice.ZOGI.GtkWidgets
{
	/* @brief Widget to display a list of enterpises */
  [System.ComponentModel.ToolboxItem(true)]
	public class EnterpriseNodeView : CompactNodeView
	{
		public EntityEventHandler OnEnterpriseEdit;
		public EntityEventHandler OnEnterpriseRemove;
		public EntityEventHandler OnEnterpriseUnassign;
		
		/* @brief Constructor */
		public EnterpriseNodeView() 
			: base(null, false, new Gtk.NodeStore(typeof(EnterpriseListNode)))
		{ SetupColumns(); }
	
		/* @brief Constructor */
		public EnterpriseNodeView(Label status, bool popup) 
			: base(status, popup, new Gtk.NodeStore(typeof(EnterpriseListNode)))
		{ SetupColumns(); }
		
		public void SetupColumns()
		{
			AppendTextColumn("Id", 0, true, false, true);
			AppendTextColumn("Name", 1, true, true, true);
			AppendTextColumn("Bank Code", 2, true, true, true);
			AppendTextColumn("URL", 3, true, true, true);
			NodeStore.Clear();
			ButtonPressEvent += new ButtonPressEventHandler(OnButonPressEvent);	
			RowActivated += new RowActivatedHandler(OnRowActivated);
		}
		
		public void Clear()
		{
			this.NodeStore.Clear();
		}
		
		/* @brief Load a new set of data into the list */
		public override void Fill(IEnumerable<Entity> _data, string _message)
		{
			this.NodeStore.Clear();
			foreach(Entity entity in _data)
		  {
		    if (entity is Enterprise)
		    {
          Registry.RegisterForNotification(entity, this);
				  this.NodeStore.AddNode(new EnterpriseListNode(entity as Enterprise));
				  //entity.OnEntityDeleted += new zOGIEntityChangeHandler(Remove);
				}
			}
			if ((_message != null) && (statusLabel != null))
				statusLabel.Text = _message;		
		} /* End Fill */
		
		public void EnterpriseEdit(object _sender, EventArgs _args)
		{ 
			OnEnterpriseEdit(
				(_sender as PopUpMenuItem).ContextObject,
				"edit",
				null); 
		}
		
		public void EnterpriseRemove(object _sender, EventArgs _args)
		{ 
			OnEnterpriseRemove(
				(_sender as PopUpMenuItem).ContextObject,
				"remove",
				null); 
		}		

		public void EnterpriseUnassign(object _sender, EventArgs _args)
		{ 
			OnEnterpriseUnassign(
				(_sender as PopUpMenuItem).ContextObject,
				"unassign",
				null); 
		}

		[GLib.ConnectBefore]
		protected void OnRowActivated(object sender, Gtk.RowActivatedArgs args)
		{
		 	ITreeNode				node;

			node = (sender as NodeView).NodeSelection.SelectedNode;
			if ((node is EnterpriseListNode) && (this.OnEnterpriseEdit != null))
	  	  OnEnterpriseEdit(((EnterpriseListNode)node).Enterprise, "edit", null);
		}
								
		/* @brief Handler for click events on the list */
		[GLib.ConnectBefore]		
		public void OnButonPressEvent(object _sender, ButtonPressEventArgs _args)
		{
			ITreeNode				node;
			Enterprise			enterprise;

			if (PopupEnabled)
			{
  			/* Get the selected list node from the nodeview
	  			 and bail out if result is not a EnterpriseListNode */
		  	node = (_sender as NodeView).NodeSelection.SelectedNode;
			  if (node is EnterpriseListNode)
				  enterprise = ((EnterpriseListNode)node).Enterprise;
				  else return;
  			/* Is the click a right-button */
	  		if (_args.Event.Button == 3) 
		  	{
  		  	PopupMenuBuilder  menu;
  
	      	menu = new PopupMenuBuilder();
	      	menu.Append("Edit", enterprise, new EventHandler(EnterpriseEdit), 
	      	            (OnEnterpriseEdit != null));
	  	    menu.AppendSeperator();
  	  	  menu.Append("Unassign", enterprise, new EventHandler(EnterpriseUnassign), 
	    	              ((OnEnterpriseUnassign != null) && (enterprise.CanModify)));
	    	  menu.AppendSeperator();
 			  	menu.Append("Delete", enterprise, new EventHandler(EnterpriseRemove), 
 				              ((OnEnterpriseRemove != null) && (enterprise.CanModify)));
   			  menu.Run();
			  } /* End if buttun pressed is the right button */
		  }
		} /* End OnButonPressEvent */
	} /* End Class EnterpriseNodeView */
} /* End Namespace */